package logintest.util;


import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;

public class DBUtils {

    //private String url="jdbc:mysql://localhost:3306/yhp3?serverTimezone=UTC";
    //private String username="root";
    //private String password="123456";
    //1.定义需要的工具类对象
    protected Connection connection = null;
    protected PreparedStatement pps = null;
    protected ResultSet resultSet = null;
    protected int count = 0;//受影响的行数

    private static String userName;
    private static String userPass;
    private static String url;
    private static String diverName;

    //德鲁伊
    private static DruidDataSource dataSource = new DruidDataSource();

    //2.加载驱动
    static{
        //try {
        //    Class.forName("com.mysql.cj.jdbc.Driver");
        //}catch (ClassNotFoundException e) {
        //    e.printStackTrace();
        //}
        ResourceBundle bundle = ResourceBundle.getBundle("db");
        diverName = bundle.getString("driverclass");
        url = bundle.getString("url");
        userName = bundle.getString("uname");
        userPass = bundle.getString("upass");

        dataSource.setUsername(userName);
        dataSource.setPassword(userPass);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(diverName);
    }
    //3.获得连接
    protected  Connection getConnection(){
        try {
            connection = dataSource.getConnection();
            //connection = DriverManager.getConnection(url,userName,userPass);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    //4.创建预通道
    protected PreparedStatement getPps(String sql){
        getConnection();
        try {
            pps = connection.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pps;
    }
    //5..给占位符赋值 list中保存的是给占位符所赋的值
    private  void setParams(List list){
        if (list != null&&list.size()>0){
            for (int i=0;i<list.size();i++) {
                try {
                    pps.setObject(i+1,list.get(i));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //6.增删改 调用的方法
    protected int update(String sql,List params){
        getPps(sql);
        setParams(params);
        try {
            count= pps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
    //7.查询的时候调用的一个方法
    protected ResultSet query(String sql,List list){
        getPps(sql);
        setParams(list);
        try {
            resultSet=pps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }
    //8.关闭资源
    protected void closeAll(){
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if(pps != null){
                pps.close();
            }
            if(connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
